package server;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.io.IOException;
import java.net.InetAddress;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author Shay
 *  Creating server GUI
 */
public class ServerGUI {

	private JFrame frame;
	private JTextField textFieldPort;
	private JLabel lblNewLabel;
	private JTextField txtRoot;
	private JTextField textFieldDBPassword;
	private JLabel lblPassword;
	private JTextField textFieldDBAddress;
	private JTextField textFieldDBname;
	public static DefaultListModel<String> listModel;
	private JButton btnNewButton;
	private EchoServer server;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ServerGUI window = new ServerGUI();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ServerGUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setBounds(100, 100, 378, 232);
		frame.getContentPane().setLayout(null);
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosed(WindowEvent arg0) {
				if (server!=null)
				{
					ArrayList<Object> s=new ArrayList<Object>();
					s.add("ServerStopped");
					server.sendToAllClients(s);
					EchoServer.ServerAlive=false;
				}
				System.exit(1);
			}
		});
		btnNewButton = new JButton("Listen");
		btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
				server = new EchoServer(Integer.parseInt(textFieldPort.getText()),textFieldDBAddress.getText(),txtRoot.getText(),textFieldDBPassword.getText(),textFieldDBname.getText());
			
					server.listen();
					btnNewButton.setEnabled(false);
					frame.setTitle(InetAddress.getLocalHost().getHostAddress()+ ":" + server.getPort());
					JOptionPane.showMessageDialog(new JFrame(),"Server Started! Listening on " + InetAddress.getLocalHost().getHostAddress()+ ":" + textFieldPort.getText());
					
				} catch (SQLException e1) {
					JOptionPane.showMessageDialog(new JFrame(),	"ERROR - SQL Connection failed!","Error",JOptionPane.ERROR_MESSAGE);
					e1.printStackTrace();
				} catch (IOException e3) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(new JFrame(),"ERROR - Could not listen for clients!");
					e3.printStackTrace();
				}
			}
		});
		btnNewButton.setBounds(15, 88, 343, 49);
		frame.getContentPane().add(btnNewButton);
		
		textFieldPort = new JTextField();
		textFieldPort.setText("5555");
		textFieldPort.setBounds(155, 147, 86, 20);
		frame.getContentPane().add(textFieldPort);
		textFieldPort.setColumns(10);
		
		JLabel lblPort = new JLabel("Port:");
		lblPort.setBounds(112, 150, 33, 14);
		frame.getContentPane().add(lblPort);
		
		lblNewLabel = new JLabel("Username:");
		lblNewLabel.setBounds(15, 61, 72, 14);
		frame.getContentPane().add(lblNewLabel);
		
		txtRoot = new JTextField();
		txtRoot.setText("root");
		txtRoot.setBounds(87, 58, 100, 20);
		frame.getContentPane().add(txtRoot);
		txtRoot.setColumns(10);
		
		textFieldDBPassword = new JTextField();
		textFieldDBPassword.setText("1234");
		textFieldDBPassword.setBounds(258, 58, 100, 20);
		frame.getContentPane().add(textFieldDBPassword);
		textFieldDBPassword.setColumns(10);
		
		lblPassword = new JLabel("Password:");
		lblPassword.setBounds(195, 61, 71, 14);
		frame.getContentPane().add(lblPassword);
		
		JLabel lblAddress = new JLabel("DB address:");
		lblAddress.setBounds(15, 32, 82, 16);
		frame.getContentPane().add(lblAddress);
		
		textFieldDBAddress = new JTextField();
		textFieldDBAddress.setBounds(87, 29, 100, 22);
		frame.getContentPane().add(textFieldDBAddress);
		textFieldDBAddress.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("DBName:");
		lblNewLabel_1.setBounds(199, 32, 56, 16);
		frame.getContentPane().add(lblNewLabel_1);
		
		textFieldDBname = new JTextField();
		textFieldDBname.setText("test");
		textFieldDBname.setBounds(258, 29, 100, 22);
		frame.getContentPane().add(textFieldDBname);
		textFieldDBname.setColumns(10);
		listModel = new DefaultListModel<String>();
	}
}
